package com.ghost.alibabaghost.web.controller;

import com.ghost.alibabaghost.api.model.sys.GhostUsersModel;
import com.ghost.alibabaghost.api.service.sys.GhostUsersService;
import com.ghost.alibabaghost.result.Code;
import com.ghost.alibabaghost.result.SingleResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author yaolisheng
 * @version 1.0
 * @date 2022/1/15 08:29
 */

@RestController
@RequestMapping("/users")
public class LoginController {

    private static final Logger logger = LoggerFactory.getLogger(LoginController.class);

    @Autowired
    private GhostUsersService ghostUsersService;

    @RequestMapping("/login")
    public SingleResult<GhostUsersModel> loginAction(@RequestBody GhostUsersModel ghostUsersModel) {
        logger.info("loginAction-->username"+ghostUsersModel.getUsername());
        try {
            GhostUsersModel usersModel = ghostUsersService.queryForLogin(ghostUsersModel.getUsername(), ghostUsersModel.getPassword());
            return SingleResult.buildSuccess(usersModel);
        } catch (Exception e) {
            logger.error("loginAction-->username"+ghostUsersModel.getUsername(),e);
            return SingleResult.buildFailure(Code.ERROR, Code.ERROR.getMessage());
        }
    }
}
